package cn.easyutil.veteran.core.utils;

import cn.hutool.core.date.DateUtil;

import java.util.Date;

/**
 * 时间类型
 */
public enum TimeEnumUtil {

    year(1,"年"){
        @Override
        public long parseMillisecond(int value) {
            Date now = new Date();
            return DateUtil.offsetMonth(now,12*value).getTime()-now.getTime();
        }
        @Override
        public int current(Date date) {
            return DateUtil.year(date);
        }

        @Override
        public long start() {
            Date now = new Date();
            return DateUtil.beginOfYear(now).getTime();
        }
    },
    month(2,"月"){
        @Override
        public long parseMillisecond(int value) {
            Date now = new Date();
            return DateUtil.offsetMonth(now,value).getTime()-now.getTime();
        }
        @Override
        public int current(Date date) {
            return DateUtil.month(date)+1;
        }

        @Override
        public long start() {
            Date now = new Date();
            return DateUtil.beginOfMonth(now).getTime();
        }
    },
    day(3,"天"){
        @Override
        public long parseMillisecond(int value) {
            System.out.println("延迟了 " + value + "天" );
            Date now = new Date();
            return DateUtil.offsetDay(now,value).getTime()-now.getTime();
        }
        @Override
        public int current(Date date) {
            return DateUtil.dayOfMonth(date);
        }

        @Override
        public long start() {
            Date now = new Date();
            return DateUtil.beginOfDay(now).getTime();
        }
    },
    hour(4,"时"){
        @Override
        public long parseMillisecond(int value) {
            Date now = new Date();
            return DateUtil.offsetHour(now,value).getTime()-now.getTime();
        }
        @Override
        public int current(Date date) {
            return DateUtil.hour(date,true);
        }

        @Override
        public long start() {
            Date now = new Date();
//            return DateUtil.getBeginValue(now).getTime();
            return 0L;
        }
    },
    minute(5,"分"){
        @Override
        public long parseMillisecond(int value) {
            Date now = new Date();
            return DateUtil.offsetMinute(now,value).getTime()-now.getTime();
        }
        @Override
        public int current(Date date) {
            return DateUtil.minute(date);
        }

        @Override
        public long start() {
            Date now = new Date();
//            return DateUtil.beginOfSecond(now).getTime();
            return 0L;
        }
    },
    second(6,"秒"){
        @Override
        public long parseMillisecond(int value) {
            Date now = new Date();
            return DateUtil.offsetSecond(now,value).getTime()-now.getTime();
        }
        @Override
        public int current(Date date) {
            return DateUtil.second(date);
        }

        @Override
        public long start() {
            Date now = new Date();
            return DateUtil.beginOfSecond(now).getTime();
        }
    },
    week(7,"周"){
        @Override
        public long parseMillisecond(int value) {
            Date now = new Date();
            return DateUtil.offsetWeek(now,value).getTime()-now.getTime();
        }
        @Override
        public int current(Date date) {
            int current = DateUtil.dayOfWeek(date)-1;
            if(current == 0){
                return 7;
            }
            return current;
        }

        @Override
        public long start() {
            Date now = new Date();
            return DateUtil.beginOfWeek(now).getTime();
        }
    };

    public final Integer type;

    public final String remark;

    TimeEnumUtil(Integer type, String...remarks) {
        this.type = type;
        this.remark = remarks.length>0?remarks[0]:"";
    }

    public static TimeEnumUtil getByType(Integer type){
        if(type == null){
            return hour;
        }
        for (TimeEnumUtil pe : TimeEnumUtil.values()) {
            if(pe.type.equals(type)){
                return pe;
            }
        }
        return hour;
    }

    /**
     * 转换成毫秒
     * @param value 对应的转换值
     */
    public abstract long parseMillisecond(int value);

    /**
     * 获取当前时间的 开始 毫秒数
     * 例 小时 当前 15:35:59 则获取 15:00:00 的毫秒值
     */
    public abstract long start();

    /**
     * 获取当前表示的数字
     */
    public int current(){
        return current(new Date());
    }

    /**
     * 判断是否一致
     */
    public boolean same(int value){
        return same(new Date(),value);
    }

    /**
     * 获取当前表示的数字
     */
    public abstract int current(Date date);


    /**
     * 判断是否相同
     * @param date  时间
     * @param value 比对值
     */
    public boolean same(Date date,int value){
        return current(date) == value;
    }

    /**
     * 时间相加
     * @param value 倍数
     */
    public Long sum(int value){
        return sum(new Date(),value);
    }

    /**
     * 时间相加
     * @param date  传入时间
     * @param value 倍数
     */
    public Long sum(Date date,int value){
        return parseMillisecond(value)+date.getTime();
    }

}
